/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.scm.hibernate.entities;

import com.scm.hibernate.utilities.HibernateUtil;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author YESHA
 */
@Table(name = "subjects")
@Entity
public class Subject extends AbstractEntity
{
    @GeneratedValue
    
    @Column(name = "id")
    @Id
    private Long id;
    
    @Column(name = "name")
    private String name;
    
    @Column(name = "addedby")
    private Long addedby;
    
    @Column(name = "code")
    private int code;
    
    @Column(name = "lecturesperweek")
    private int lecturesperweek;
    
    @Column(name = "labsperweek")
    private int labsperweek;

    public int getLecturesperweek() {
        return lecturesperweek;
    }

    public void setLecturesperweek(int lecturesperweek) {
        this.lecturesperweek = lecturesperweek;
    }

    public int getLabsperweek() {
        return labsperweek;
    }

    public void setLabsperweek(int labsperweek) {
        this.labsperweek = labsperweek;
    }
    
    
    

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getAddedby() {
        return addedby;
    }

    public void setAddedby(Long addedby) {
        this.addedby = addedby;
    }

    
    
    
     public  Subject getByName() {
        Subject sub = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
             Query query = session.createQuery("from Subject where name= '"+this.getName()+"' ");
            List<Subject> li=query.list();
            
            if(li.size()==1)
            {
              sub=li.get(0);
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sub;
    }
public static List<Subject> getallsubjects() {
        List<Subject> li = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();

            Query q = s.createQuery("from Subject");

            li = q.list();
          
        } catch (Exception e) {
               e.printStackTrace();
        }
        return li;
    }
    
}
